home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / glchess / cecp.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  8.8 KB  |  248 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __author__ = 'Robert Ancell <bob27@users.sourceforge.net>'
  5. __license__ = 'GNU General Public License Version 2'
  6. __copyright__ = 'Copyright 2005-2006  Robert Ancell'
  7.  
  8. class CECPProtocol:
  9.     '''CECP protocol en/decoder.
  10.     
  11.     
  12.     '''
  13.     __buffer = ''
  14.     NEWLINE = '\n'
  15.     MOVE_PREFIXS = [
  16.         'My move is: ',
  17.         'my move is ',
  18.         'move ']
  19.     INVALID_MOVE_PREFIX = 'Illegal move: '
  20.     RESIGN_PREFIX = 'tellics resign'
  21.     DRAW_PREFIX = '1/2-1/2'
  22.     
  23.     def __init__(self):
  24.         '''
  25.         '''
  26.         self.onOutgoingData('xboard\n')
  27.  
  28.     
  29.     def onOutgoingData(self, data):
  30.         """Called when there is data to send to the CECP engine.
  31.         
  32.         'data' is the data to give to the AI (string).
  33.         """
  34.         print 'OUT: ' + repr(data)
  35.  
  36.     
  37.     def onUnknownLine(self, line):
  38.         """Called when an unknown line is received from the CECP AI.
  39.         
  40.         'line' is the line that has not been decoded (string). There is
  41.                no newline on the end of the string.
  42.         """
  43.         print 'Unknown CECP line: ' + line
  44.  
  45.     
  46.     def onMove(self, move):
  47.         """Called when the AI makes a move.
  48.         
  49.         'move' is the move the AI has decided to make (string).
  50.         """
  51.         print 'CECP move: ' + move
  52.  
  53.     
  54.     def onIllegalMove(self, move):
  55.         """Called when the AI rejects a move.
  56.         
  57.         'move' is the move the AI rejected (string).
  58.         """
  59.         print 'CECP illegal move: ' + move
  60.  
  61.     
  62.     def onResign(self):
  63.         '''Called when the AI resigns'''
  64.         print 'CECP AI resigns'
  65.  
  66.     
  67.     def logText(self, text, style):
  68.         print 'LOG: %s' % text
  69.  
  70.     
  71.     def sendSetSearchDepth(self, searchDepth):
  72.         """Set the search depth for the AI.
  73.         
  74.         'searchDepth' is the number of moves to look ahead (integer).
  75.         """
  76.         self.onOutgoingData('sd %i\n' % int(searchDepth))
  77.         self.onOutgoingData('depth %i\n' % int(searchDepth))
  78.  
  79.     
  80.     def sendSetPondering(self, aiPonders):
  81.         """Enable/disable AI pondering.
  82.         
  83.         'aiPonders' is a flag to show if the AI thinks during opponent moves (True) or not (False).
  84.         """
  85.         if aiPonders:
  86.             self.onOutgoingData('hard\n')
  87.         else:
  88.             self.onOutgoingData('easy\n')
  89.  
  90.     
  91.     def sendMove(self, move):
  92.         """Move for the current player.
  93.         
  94.         'move' is the move the current player has made (string).
  95.         """
  96.         self.onOutgoingData(move + '\n')
  97.  
  98.     
  99.     def sendWait(self):
  100.         '''Stop the AI from automatically moving'''
  101.         self.onOutgoingData('force\n')
  102.  
  103.     
  104.     def sendUndo(self):
  105.         '''Undo the last move'''
  106.         self.onOutgoingData('undo\n')
  107.  
  108.     
  109.     def sendMovePrompt(self):
  110.         '''Get the AI to move for the current player'''
  111.         self.onOutgoingData('go\n')
  112.  
  113.     
  114.     def sendConventionalClock(self, moveCount, base, increment):
  115.         """
  116.         
  117.         'moveCount' ???
  118.         'base' ??? (seconds)
  119.         'increment' ??? (seconds)
  120.         """
  121.         self.onOutgoingData('level %d %d:%02d %d:%02d\n' % (moveCount, base / 60, base % 60, increment / 60, increment % 60))
  122.  
  123.     
  124.     def sendQuit(self):
  125.         '''Quit the engine'''
  126.         self.onOutgoingData('\nquit\n')
  127.  
  128.     
  129.     def registerIncomingData(self, data):
  130.         '''
  131.         '''
  132.         self._CECPProtocol__buffer += data
  133.         self._CECPProtocol__parseData()
  134.  
  135.     
  136.     def __parseData(self):
  137.         while True:
  138.             index = self._CECPProtocol__buffer.find(self.NEWLINE)
  139.             if index < 0:
  140.                 return None
  141.             line = self._CECPProtocol__buffer[:index]
  142.             self._CECPProtocol__buffer = self._CECPProtocol__buffer[index + 1:]
  143.             self._CECPProtocol__parseLine(line)
  144.             continue
  145.             index < 0
  146.  
  147.     
  148.     def __parseLine(self, line):
  149.         for prefix in self.MOVE_PREFIXS:
  150.             if line.startswith(prefix):
  151.                 move = line[len(prefix):]
  152.                 self.logText(line + '\n', 'move')
  153.                 self.onMove(move.strip())
  154.                 return None
  155.         
  156.         if line.startswith(self.INVALID_MOVE_PREFIX):
  157.             self.onIllegalMove(line[len(self.INVALID_MOVE_PREFIX):])
  158.         elif line.startswith(self.RESIGN_PREFIX):
  159.             self.onResign()
  160.         elif line.startswith(self.DRAW_PREFIX):
  161.             print 'AI calls a draw'
  162.         else:
  163.             self.onUnknownLine(line)
  164.         self.logText(line + '\n', 'input')
  165.  
  166.  
  167.  
  168. class Connection(CECPProtocol):
  169.     '''
  170.     '''
  171.     
  172.     def __init__(self):
  173.         '''
  174.         '''
  175.         CECPProtocol.__init__(self)
  176.  
  177.     
  178.     def logText(self, text, style):
  179.         '''FIXME: define style
  180.         '''
  181.         pass
  182.  
  183.     
  184.     def onMove(self, move):
  185.         """Called when the AI makes a move.
  186.         
  187.         'move' is the move the AI made (string).
  188.         """
  189.         print 'AI moves: ' + move
  190.  
  191.     
  192.     def start(self):
  193.         '''
  194.         '''
  195.         pass
  196.  
  197.     
  198.     def startGame(self):
  199.         '''
  200.         '''
  201.         pass
  202.  
  203.     
  204.     def configure(self, options = []):
  205.         '''
  206.         '''
  207.         for option in options:
  208.             self.onOutgoingData(option.value + '\n')
  209.         
  210.  
  211.     
  212.     def requestMove(self, whiteTime, blackTime, ownTime):
  213.         '''Request the AI moves for the current player'''
  214.         if ownTime > 0:
  215.             self.sendConventionalClock(0, ownTime / 1000, 0)
  216.         
  217.         self.sendMovePrompt()
  218.  
  219.     
  220.     def undoMove(self):
  221.         '''Undo the last move made by this AI'''
  222.         self.sendWait()
  223.         self.sendUndo()
  224.  
  225.     
  226.     def reportMove(self, move, isSelf):
  227.         """Report the move the current player has made.
  228.         
  229.         'move' is the move to report (string).
  230.         'isSelf' is a flag to say if the move is the move this AI made (True).
  231.         """
  232.         if isSelf:
  233.             return None
  234.         self.sendWait()
  235.         self.sendMove(move)
  236.  
  237.     
  238.     def onUnknownLine(self, line):
  239.         '''Called by CECPProtocol'''
  240.         pass
  241.  
  242.     
  243.     def onIllegalMove(self, move):
  244.         '''Called by CECPProtocol'''
  245.         print 'CECP illegal move: ' + move
  246.  
  247.  
  248.